home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / human interface toolbox / progressbars / nonthreadedprogress.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  4.6 KB  |  224 lines

  1. /*
  2.     File:        NonThreadedProgress.c
  3.  
  4.     Contains:    Progress bar implementation without using the Threads Manager
  5.  
  6.     Written by: Chris White    
  7.  
  8.     Copyright:    Copyright © 1996-1999 by Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.                 8/10/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  20.                 
  21.  
  22. */
  23.  
  24. #pragma segment Core
  25.  
  26.  
  27. // System Includes
  28.  
  29. #ifndef __DIALOGS__
  30.     #include <Dialogs.h>
  31. #endif
  32.  
  33.  
  34.  
  35. // Application Includes
  36.  
  37. #ifndef __BAREBONES__
  38.     #include "BareBones.h"
  39. #endif
  40.  
  41. #ifndef __PROTOTYPES__
  42.     #include "ProtoTypes.h"
  43. #endif
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50. // static prototypes
  51. static void        DrawProgressBar ( DialogRef theDialog, int doneAmount, int maxAmount );
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58. OSErr ProgressOperation ( tOperation theOperation, void* refCon, StringPtr theText )
  59. {
  60.     Boolean            bCancelled = false;
  61.     OSErr            theErr = noErr;
  62.     SInt16            theType;
  63.     GrafPtr            savePort;
  64.     DialogRef        theDialog = nil;
  65.     Handle            theHan;
  66.     Rect            theRect;
  67.     
  68.     
  69.     theDialog = GetNewDialog ( kProgressDialogID, nil, (WindowPtr) -1 );
  70.     
  71.     GetPort ( &savePort );
  72.     SetPort ( theDialog );
  73.     
  74.     GetDialogItem ( theDialog, kStaticTextItemID, &theType, &theHan, &theRect );
  75.     SetDialogItemText ( theHan, theText );
  76.     
  77.     ShowWindow ( theDialog );
  78.     DrawDialog ( theDialog );
  79.     
  80.     bCancelled = (*theOperation) ( refCon, theDialog );
  81.     
  82.     DisposeDialog ( theDialog );
  83.     SetPort ( savePort );
  84.     
  85.     return theErr;
  86.     
  87. } // ProgressOperation
  88.  
  89.  
  90.  
  91. Boolean UpdateProgressBar ( DialogRef theDialog, int doneAmount, int maxAmount )
  92. {
  93.     Boolean            bCancelled = false;
  94.     OSErr            theErr = noErr;
  95.     EventRecord        theEvent;
  96.     
  97.     
  98.     
  99.     if ( WaitNextEvent ( everyEvent - diskMask, &theEvent, kSleepTime, nil ) )
  100.     {
  101.         if ( IsDialogEvent ( &theEvent ) )
  102.         {
  103.             SInt16        itemHit;
  104.             DialogRef    whichDialog;
  105.             
  106.             if ( DialogSelect ( &theEvent, &whichDialog, &itemHit ) )
  107.             {
  108.                 if ( whichDialog == theDialog && itemHit == kCancelItemID )
  109.                     bCancelled = true;
  110.             }
  111.         }
  112.         else if ( theEvent.what == mouseDown )
  113.         {
  114.             WindowRef    theWindow;
  115.             
  116.             if ( FindWindow ( theEvent.where, &theWindow ) == inDrag )
  117.                 DoDragWindow ( theWindow, &theEvent );
  118.         }
  119.     }
  120.     
  121.     DrawProgressBar ( theDialog, doneAmount, maxAmount );
  122.     
  123.     return bCancelled;
  124.     
  125. } // UpdateProgressBar
  126.  
  127.  
  128.  
  129. static void DrawProgressBar ( DialogRef theDialog, int doneAmount, int maxAmount )
  130. {
  131.     SInt16            theType;
  132.     int                theLength;
  133.     float            floatDone, floatMax, thePercent;
  134.     Handle            theHan;
  135.     Rect            theRect;
  136.     
  137.     
  138.     
  139.     GetDialogItem ( theDialog, kUserItemID, &theType, &theHan, &theRect );
  140.     if ( maxAmount )        // Standard Progress Bar
  141.     {
  142.         SetDialogItem ( theDialog, kUserItemID, theType, (Handle) gOutlineUserItemUPP, &theRect );
  143.         // call it now, so that after an update event the boarder
  144.         // is drawn before FillRect is called
  145.         CallUserItemProc ( gOutlineUserItemUPP, theDialog, kUserItemID );
  146.         
  147.         
  148.         theLength = theRect.right - theRect.left;
  149.         
  150.         floatDone = doneAmount;
  151.         floatMax = maxAmount;
  152.         thePercent = (floatDone / floatMax) * 100;
  153.         theRect.right = theRect.left + ((thePercent / 100) * theLength);
  154.         FillRect ( &theRect, &qd.black );
  155.     }
  156.     else                    // Barber Pole Progress Bar
  157.     {
  158.         static SInt16    theID = 1000;
  159.         PicHandle        thePic;
  160.         
  161.         
  162.         thePic = GetPicture ( theID++ );
  163.         DrawPicture ( thePic, &theRect );
  164.         if ( theID > 1003 )
  165.             theID = 1000;
  166.     }
  167.     
  168.     return;
  169.     
  170. } // DrawProgressBar
  171.  
  172.  
  173.  
  174. //
  175. // This routine is one of the operations carried out
  176. // which the progress bar is representing.
  177. //
  178. Boolean StandardDemoOperation ( void* refCon, DialogRef theDialog )
  179. {
  180.     #pragma unused(refCon)
  181.     Boolean        bWasCancelled = false;
  182.     int            i;
  183.     const int    max = 100;
  184.     
  185.     
  186.     for ( i = 1; i <= max && bWasCancelled == false; i++ )
  187.     {
  188.         UInt32    theDelay = 10L;
  189.         Delay ( theDelay, &theDelay );
  190.         bWasCancelled = UpdateProgressBar ( theDialog, i, max );
  191.     }
  192.     
  193.     return bWasCancelled;
  194. }
  195.  
  196.  
  197.  
  198. //
  199. // This routine is one of the operations carried out
  200. // which the progress bar is representing.
  201. //
  202. Boolean BarberPoleDemoOperation ( void* refCon, DialogRef theDialog )
  203. {
  204.     #pragma unused(refCon)
  205.     Boolean        bWasCancelled = false;
  206.     int            i;
  207.     const int    max = 100;
  208.     
  209.     
  210.     for ( i = 1; i <= max && bWasCancelled == false; i++ )
  211.     {
  212.         UInt32    theDelay = 10L;
  213.         Delay ( theDelay, &theDelay );
  214.         bWasCancelled = UpdateProgressBar ( theDialog, 0, 0 );
  215.     }
  216.     
  217.     return bWasCancelled;
  218. }
  219.  
  220.  
  221.  
  222.  
  223.  
  224.